layui.extend({
    loading: '../../../plugins/layui_extends/loading/loading',
}).define([ 'form', 'laydate', 'table','loading'], function(exports) {
    var form = layui.form;
    var cpTaskSetupApi = layui.cpTaskSetupApi;
    var index = layui.index;
    var isNew = true;
    var tableIns;
    var table = layui.table;
    // initTable();
    var view = {
        init:function(){
            this.initTable();
            this.initSubmit();
            Lib.initGenrealForm($("#addForm"),form);

        },
        initTable:function(){
            var result = new Array();
            result = getdata();
            console.log(result);
            tableIns = table.render({
                elem: '#dataTable'
                ,height: 'full'
                ,width : 500
                // ,url: '${ctxPath}/plandata/tDPlanningData/getMajor.json?year='+year+'&type='+type+'&provinceId='+provinceId+'&univId='+univId+'&batchId='+batchId //数据接口
                ,data: result
                ,page: false //开启分页
                ,limit:100
                ,cols: [[ //表头
                    {field: 'ruleName', title: '规则名称',edit: 'text'}
                    ,{field: 'minValue', title: '大于等于', edit: 'text'}
                    ,{field: 'maxValue', title: '小于<' ,edit: 'number'}
                    ,{field: 'maxQuota', title: '最大名额',edit: 'text'}
                    ,{field: '', title: '操作',
                        templet: function(d){
                            return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id=""><i class="layui-icon layui-icon-delete"></i>移除</a>';
                        }
                    }

                ]]
                ,done:function (res, curr, count) {
                }
            });

            table.on('tool(dataTable)', function (obj) {
                var  data = obj.data,event = obj.event; //获得当前行 tr 的DOM对象;
                switch(event){
                    case "del":
                        layer.confirm('确认删除该行？', function(index){
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);
                            var oldData = table.cache['dataTable']
                            oldData.splice(obj.tr.data('index'),1);
                            tableIns.reload({
                                data : oldData
                            });
                        });
                        break;

                }
            });
        },
        initSubmit:function(){
            $("#addButton").click(function(){
                // $("#addButton").hide();
                // $("#addButton-loading").show();
                // $("#addButton-loading").loading();
                form.on('submit(form)', function(){
                    // cpTaskSetupApi.saveForm(isNew,$('#addForm'),function(){
                    //     // parent.window.dataReload();
                    //     // Common.info("保存成功");
                    //     // Lib.closeFrame();
                    // });


                    // var data =  $("#addForm").serializeJson();
                    var majorData =table.cache['dataTable']
                    // data.rules=JSON.stringify(majorData);

                    $.ajax({
                        type: 'POST',
                        url: Common.ctxPath +'/evaluation/cpTaskRule/save.json',
                        data: {
                            taskId :$("#taskId").val(),
                            rules : JSON.stringify(majorData)
                        },
                        async: false,
                        dataType:'json',
                        success: function(data){
                            if(data.code == 0){
                                Common.info("保存成功");
                                parent.callback();
                            }else{
                                Common.info(data.msg);
                            }
                        }
                    });
                });
            });

            $("#addButton-cancel").click(function(){
                Lib.closeFrame();
            });

        }

    }

    $(document).on('click','#addRow',function(obj){
        var oldData = table.cache['dataTable'];
        if(oldData==null||oldData==''){
            oldData = [];
        }
        var newRow = {
            ruleName:null,
            minValue:null,
            maxValue:null,
            maxQuota:null
        };

        oldData.push(newRow);

        tableIns.reload({
            data : oldData
        });
    });

    //监听工具条

    function initTable(){

    }

    function getdata(){
        var res;
        var taskId = $("#taskId").val();
        $.ajax({
            type: 'POST',
            url: 'getCpTaskRule.json',
            data: {
                taskId:taskId
            },
            async: false,
            dataType:'json',
            success: function(data){
                res= data.data;
            }
        });
        return res;
    }

    exports('rule',view);
});
